/**
 *  DBEXEL is a Database Backed & Web-Based Worksheet and chart management program. 
 *  It has been influenced by Excel.
 *  For questions or suggestions please contact the developper at ( Development@Gandomi.com )
 *  Copyright (C) 2011 Baubak Gandomi   
 *
 *	This file is part of the application DBEXEL
 *
 *   DBEXEL is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   DBEXEL is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package dbexel.presentation.aat;

import static org.junit.Assert.*;

import java.io.IOException;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebAssert;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlButton;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlTable;

import dbexel.system.tools.DBExelTestTools;

/**
 * @author gandomi
 * 
 */
public class EditWorkSheetEntryAAT {
	private WebClient webClient;

	@Before
	public void initSystem() {
		webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
		webClient.setJavaScriptEnabled(true);
		webClient.getCookieManager().setCookiesEnabled(true);

	}

	@After
	public void cleanWindows() {
		webClient.closeAllWindows();
	}

	@Test
	public void testAddAttributeValue() throws FailingHttpStatusCodeException,
			IOException {

		HtmlPage l_currentPage = (HtmlPage) webClient
				.getPage(getClass()
						.getResource(
								"/testExamples/scenarios/WorkSheetDetails/EditWorkSheetEntry.action.htm"));

		WebAssert.assertTitleEquals(l_currentPage, "Update Worksheet Entry");

		HtmlTable l_currentTable = DBExelTestTools.getTableByOrder(
				l_currentPage, 1);

		assertEquals("contentTable", l_currentTable.getId());

		assertEquals(2, l_currentTable.getRowCount());

		assertEquals(3, l_currentTable.getRow(0).getCells().size());
		assertEquals(3, l_currentTable.getRow(1).getCells().size());

		HtmlButton l_button = l_currentPage.getDocumentElement()
				.getOneHtmlElementByAttribute("button", "id", "addAttrValue");
		assertEquals("+", l_button.getTextContent());

		// Now let us see if we click on the button do we get the new input
		// fields?
		l_button.click();

		assertEquals(2, l_currentTable.getRowCount());
		assertEquals(4, l_currentTable.getRow(0).getCells().size());
		assertEquals(4, l_currentTable.getRow(1).getCells().size());

		assertTrue(l_currentTable.getRow(0).getCell(3).getFirstChild() instanceof HtmlInput);
		HtmlInput l_attributeInput = (HtmlInput) l_currentTable.getRow(0)
				.getCell(3).getFirstChild();

		assertEquals("newAttribute[0]", l_attributeInput.getNameAttribute());

		assertTrue(l_currentTable.getRow(1).getCell(3).getFirstChild() instanceof HtmlInput);
		l_attributeInput = (HtmlInput) l_currentTable.getRow(1).getCell(3)
				.getFirstChild();

		assertEquals("newAttributeValue[0]",
				l_attributeInput.getNameAttribute());

		// Now let us see if we click on the button once again
		l_button.click();
		assertEquals(5, l_currentTable.getRow(0).getCells().size());
		assertEquals(5, l_currentTable.getRow(1).getCells().size());
		
		l_attributeInput = (HtmlInput) l_currentTable.getRow(0)
				.getCell(4).getFirstChild();

		assertEquals("newAttribute[1]", l_attributeInput.getNameAttribute());

		assertTrue(l_currentTable.getRow(1).getCell(4).getFirstChild() instanceof HtmlInput);
		l_attributeInput = (HtmlInput) l_currentTable.getRow(1).getCell(4)
				.getFirstChild();

		assertEquals("newAttributeValue[1]",
				l_attributeInput.getNameAttribute());

	}
}
